package com.mytest.offer;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;

@Slf4j
public class SolutionJZ6 {

    public static void main(String[] args) {
        SolutionJZ6 solutionJZ6 = new SolutionJZ6();
        ListNode listNode = new ListNode(1);
        ListNode listNode1 = new ListNode(2);
        ListNode listNode2 = new ListNode(3);

        listNode.next = listNode1;
        listNode1.next = listNode2;

        solutionJZ6.printListFromTailToHead(listNode);
    }

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> integers = new ArrayList<>();
        if (listNode == null) {
            return integers;
        }
        ListNode currentNode = listNode.next;
        listNode.next = null;
         while(currentNode != null){
             ListNode next = currentNode.next;
             //当前节点的next指向上一节点
             currentNode.next = listNode;
             listNode = currentNode;
             if (next != null) {
                 currentNode = next;
             }else{
                 break;
             }
         }


         while(currentNode != null){
             integers.add(currentNode.val);
             log.info("{}", currentNode.val);
             currentNode = currentNode.next;
         }
        return integers;
    }
}

class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }
